<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="search"></el-input>
      <el-button @click="empty">清除</el-button>
      <el-button type="primary" @click="searchBtn">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{row}">
          <el-button size="mini" style="margin-right: 10px" @click="edit(row.id)">编辑</el-button>
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="del(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination 
      layout="prev, pager, next" 
      :total="total"
      :page-size="information._limit"
      @current-change="pageChange"
      > </el-pagination>
      <el-button size="small" plain @click="addRole">+ 新增</el-button>
    </div>
    <el-dialog
      ref="addRole"
      @close-dialog="closeDialog"
      title="提示"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form  
      :model="ruleForm" 
      :rules="rules" 
      ref="ruleForm" 
      label-width="100px" >
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="ruleForm.des"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 获取 删除 新增 编辑 详情 搜索
import {getInformation , portRemove , getIncreased , portCompile ,portDetails,portSearch} from '@/api/connector.js'
import { v4 as uuidv4 } from 'uuid'

export default {
  data() {
    return {
      search:'',
      list:[],
      information:{
        _page: 1,  // 当前第几页
        _limit: 5  // 每页条数
      },
      total:0,
       dialogVisible: false,
       ruleForm:{
          name:'',
          des:'',
          id: ''
       },
       rules:{
         name: [
            { required: true, message: '请输入任务名称', trigger: 'blur' },
            
          ],
          des:[
            { required: true, message: '请输入任务描述', trigger: 'blur' },
            
          ]
       }
    }
  },
  mounted(){
    this.abc()
  },
  methods:{
     table_index(index) {
      return (this.information.page - 1) * this.information.pagesize + index + 1
    },
    // 渲染
    async abc(){
      const res = await getInformation(this.information)
      console.log(res)
      this.list = res.data
      this.total = Number(res.headers['x-total-count'])
      // console.log(res.headers['x-total-count']);
    },
    pageChange(value){
      console.log(value);
      this.information._page = value
      this.abc()
    },
    // 打开弹窗
    addRole(){
      this.dialogVisible = true
    },
    // 关闭弹窗
    closeDialog(){
      this.dialogVisible =false
    },
    // 删除
    async del(id){
      await portRemove(id)
      this.$message.success('删除成功')
      this.abc()
    },
    // 确定按钮
    confirm(){
     console.log(uuidv4());
     this.$refs.ruleForm.validate(async(valid)=>{
      if (valid) {
          if (this.ruleForm.id) {
            await portCompile(this.ruleForm)
          } else {
            // 调用新增接口
            await getIncreased(this.ruleForm)
          }
          // 1. 关闭弹框  2.更新列表  3.提示用户 4.清楚表单[痕迹加上表单数据]
          this.$message.success(`${this.ruleForm.id ? '编辑' : '新增'}成功`)
          this.dialogVisible =false
          this.abc()
          this.ruleForm.name=''
          this.ruleForm.des=''
        }
     })
    },
    // 编辑
    async edit(id){
      this.dialogVisible = true
      const res = await portDetails(id)
      console.log(res);
      this.ruleForm = res.data
    },
    // 搜索框
    async searchBtn(){
      const res = await portSearch(this.search)
      console.log(res);
      this.list = res.data
      if(this.search===''){
        this.list = []
      }
    },
    // 清空
    empty(){
      this.search = ''
      this.abc()
    }
  }
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
